
<?
	  include("/inc/header.html") {}
    ?>
 <script type="text/javascript" src="static/My97DatePicker/WdatePicker.js"></script>
	<div id="page-wrapper" style="margin-left: 0px; ">
		<div class="panel ">
			<input type="hidden" value="" id="inputid"/>
			<input type="hidden" value="" id="types"/>
			<input type="hidden" value="" id="memberid"/>
			<div class="row" style="margin-bottom: 10px;height:380px;" id="multplediv"></div>
		</div>
	</div>

<script>
	var indexid = window.parent.getlay();
	var type = window.parent.getType();
	var memberId = window.parent.getMemberId();
	var nameZh = window.parent.getNameZh();
	var detailId = window.parent.getDetailId();
	$("#memberid").val(memberId);
	var productDetail = "";
	$(function() {
		var text ="";
	    if(type == 'balance'){
	    	text +="<div class='col-lg-12' ><input id='balance_project' type='hidden' value='"+indexid+"' class='form-control' /><div class='panel-heading'></div><div class='panel-body pan'><h4>&nbsp;&nbsp;"+nameZh+"（balance）</h4>";
	  		text +="<div class='col-lg-6' style='width: 380px; float:left; margin-right:-20px;display:inline;'>"
				 + "<label for='inputUsername' class='col-md-3 control-label'>生效时间<span class='require'>*</span> </label>"
				 + "<div class='col-md-9'><div class='input-icon'>"
				 + "<input id='balance_start' type='text' class='form-control' value='' onchange=\"dateJudge('balance','"+indexid+"')\" onclick=\"WdatePicker({skin:'whyGreen',dateFmt:'yyyy-MM-dd HH:mm:ss'})\" />"
				 +"</div></div></div>";	
			text +="<div class='col-lg-4' style='width: 380px; float:left; margin-right:-20px;display:inline;'>"
				 + "<label for='inputUsername' class='col-md-3 control-label'>失效时间<span class='require'>*</span> </label>"
				 + "<div class='col-md-9'><div class='input-icon'>"
				 + "<input id='balance_end' type='text' class='form-control' value='' onchange=\"timeJudge('balance')\" onclick=\"WdatePicker({skin:'whyGreen',dateFmt:'yyyy-MM-dd HH:mm:ss'})\" />"
				 +"</div></div></div>";	
	    	text +="<div class='col-lg-6' style='width: 380px; float:left; margin-right:-20px;display:inline;'>"
				 + "<label for='inputUsername' class='col-md-3 control-label'>余额<span class='require'>*</span> </label>"
				 + "<div class='col-md-9'><div class='input-icon'>"
				 + "<input id='balance_balance' type='text' class='form-control' value='' />"
				 +"</div></div></div>";
			 text +="<div class='col-lg-12' style='width: 580px; float:left; margin-right:-20px;display:inline;'>"
			 text += "<label for='inputUsername' class='col-md-3 control-label'>产品库<span class='require'></span> </label>"
			 text += "<div class='col-md-9'><div class='input-icon'><ul>";
				$.ajax({
					type : "post",
					url : "organizationUserProduct.do",
					data:{
						"projectId":indexid
					},
					async: false,
					success: function(data){
						for(var i in data){
							var name = data[i].name;
								text += "<li style='float:left;margin-right:30px;list-style-type:none;'><input id='balance"+data[i].id+"' name='checkall' type='checkbox' value='"+data[i].id+"'/>&nbsp;&nbsp;<label for='balance"+data[i].id+"'>"+name+"</label></li>";
						} 
					}
				});
			 text += "</ul></div></div></div>";
			    
			 text +="</div></div></div>";
	    }
	      if(type == 'time'){
	    	text +="<div class='col-lg-12' ><input id='time_project' type='hidden' value='"+indexid+"' class='form-control' /><div class='panel-heading'></div><div class='panel-body pan'><h4>&nbsp;&nbsp;"+nameZh+"（time）</h4>";
	  		text +="<div class='col-lg-6' style='width: 380px; float:left; margin-right:-20px;display:inline;'>"
				 + "<label for='inputUsername' class='col-md-3 control-label'>生效时间<span class='require'>*</span> </label>"
				 + "<div class='col-md-9'><div class='input-icon'>"
				 + "<input id='time_start' type='text' class='form-control' value='' onchange=\"dateJudge('time','"+indexid+"')\" onclick=\"WdatePicker({skin:'whyGreen',dateFmt:'yyyy-MM-dd HH:mm:ss'})\" />"
				 +"</div></div></div>";	
			text +="<div class='col-lg-4' style='width: 380px; float:left; margin-right:-20px;display:inline;'>"
				 + "<label for='inputUsername' class='col-md-3 control-label'>失效时间<span class='require'>*</span> </label>"
				 + "<div class='col-md-9'><div class='input-icon'>"
				 + "<input id='time_end' type='text' class='form-control' value='' onchange=\"timeJudge('time')\" onclick=\"WdatePicker({skin:'whyGreen',dateFmt:'yyyy-MM-dd HH:mm:ss'})\" />"
				 +"</div></div></div>";	
			 text +="<div class='col-lg-12' style='width: 580px; float:left; margin-right:-20px;display:inline;'>"
		    text += "<label for='inputUsername' class='col-md-3 control-label'>产品库<span class='require'></span> </label>"
			text += "<div class='col-md-9'><div class='input-icon'><ul>";
			$.ajax({
				type : "post",
				url : "organizationUserProduct.do",
				data:{
					"projectId":indexid
				},
				async: false,
				success: function(data){
					for(var i in data){
						var name = data[i].name;
							text += "<li style='float:left;margin-right:30px;list-style-type:none;'><input id='time"+data[i].id+"' name='checkall' type='checkbox' value='"+data[i].id+"'/>&nbsp;&nbsp;<label for='time"+data[i].id+"'>"+name+"</label></li>";
					} 
				}
			});
			text += "</ul></div></div></div>";
		    
		    text +="</div></div></div>";
	    } 
	     if(type == 'count'){
	    	text +="<div class='col-lg-12' ><input id='count_project' type='hidden' value='"+indexid+"' class='form-control' /><div class='panel-heading'></div><div class='panel-body pan'><h4>&nbsp;&nbsp;"+nameZh+"（count）</h4>";
	  		text +="<div class='col-lg-6' style='width: 380px; float:left; margin-right:-20px;display:inline;'>"
				 + "<label for='inputUsername' class='col-md-3 control-label'>生效时间<span class='require'>*</span> </label>"
				 + "<div class='col-md-9'><div class='input-icon'>"
				 + "<input id='count_start' type='text' class='form-control' value='' onchange=\"dateJudge('count','"+indexid+"')\" onclick=\"WdatePicker({skin:'whyGreen',dateFmt:'yyyy-MM-dd HH:mm:ss'})\" />"
				 +"</div></div></div>";	
			text +="<div class='col-lg-4' style='width: 380px; float:left; margin-right:-20px;display:inline;'>"
				 + "<label for='inputUsername' class='col-md-3 control-label'>失效时间<span class='require'>*</span> </label>"
				 + "<div class='col-md-9'><div class='input-icon'>"
				 + "<input id='count_end' type='text' class='form-control' value='' onchange=\"timeJudge('count')\" onclick=\"WdatePicker({skin:'whyGreen',dateFmt:'yyyy-MM-dd HH:mm:ss'})\" />"
				 +"</div></div></div>";	
	    	text +="<div class='col-lg-6' style='width: 380px; float:left; margin-right:-20px;display:inline;'>"
				 + "<label for='inputUsername' class='col-md-3 control-label'>剩余次数<span class='require'>*</span> </label>"
				 + "<div class='col-md-9'><div class='input-icon'>"
				 + "<input id='count_surplus' type='text' class='form-control' value='' />"
				 +"</div></div></div>";
		/* 	text +="<div class='col-lg-6' style='width: 380px; float:left; margin-right:-20px;display:inline;'>"
				 + "<label for='inputUsername' class='col-md-3 control-label'>累计消费次数<span class='require'>*</span> </label>"
				 + "<div class='col-md-9'><div class='input-icon'>"
				 + "<input id='count_accumulative' type='text' class='form-control' value='' />"
				 +"</div></div></div>"; */
			 text +="<div class='col-lg-12' style='width: 580px; float:left; margin-right:-20px;display:inline;'>"
		     text += "<label for='inputUsername' class='col-md-3 control-label'>产品库<span class='require'></span> </label>"
			 text += "<div class='col-md-9'><div class='input-icon'><ul>";
			 $.ajax({
				type : "post",
				url : "organizationUserProduct.do",
				data:{
					"projectId":indexid
				},
				async: false,
				success: function(data){
					for(var i in data){
						var name = data[i].name;
							text += "<li style='float:left;margin-right:30px;list-style-type:none;'><input id='count"+data[i].id+"' name='checkall' type='checkbox' value='"+data[i].id+"'/>&nbsp;&nbsp;<label for='count"+data[i].id+"'>"+name+"</label></li>";
					} 
				}
			 });
			 text += "</ul></div></div></div>";
		    
		     text +="</div></div></div>";
	    }
	    $("#multplediv").prepend(text);
	    $.ajax({
			type : "post",
			url : "organizationUserUploadQuery.do",
			data:{
				"type":type,
				"projectId":indexid,
				"memberId":memberId,
				"detailId":detailId
			},
			dataType : "json",
			success: function(data){
				if(type == 'balance'){
					for ( var i = 0; i < data.length; i++) {
						$("#inputid").val(data[i].id);
						$("#types").val(type);
						$("#balance_start").val(data[i].startTim);
						$("#balance_end").val(data[i].endTime);
						$("#balance_balance").val(data[i].balance);
						var projectProducts = data[i].projectProducts.split(";");
						for ( var j = 0; j < projectProducts.length; j++) {
							$("input[value=" + projectProducts[j] + "]").attr("checked",true);
						}
					}
				}
				if(type == 'time'){
					for ( var i = 0; i < data.length; i++) {
						$("#inputid").val(data[i].id);
						$("#types").val(type);
						$("#time_start").val(data[i].startTim);
						$("#time_end").val(data[i].endTime);
						var projectProducts = data[i].projectProducts.split(";");
						for ( var j = 0; j < projectProducts.length; j++) {
							$("input[value=" + projectProducts[j] + "]").attr("checked",true);
						}
					}
				}
				if(type == 'count'){
					for ( var i = 0; i < data.length; i++) {
						$("#inputid").val(data[i].id);
						$("#types").val(type);
						$("#count_start").val(data[i].startTim);
						$("#count_end").val(data[i].endTime);
						$("#count_surplus").val(data[i].count);
						/* $("#count_accumulative").val(data[i].countConsume); */
						var projectProducts = data[i].projectProducts.split(";");
						for ( var j = 0; j < projectProducts.length; j++) {
							$("input[value=" + projectProducts[j] + "]").attr("checked",true);
						}
					}
				}
			}
		 });
	});

	function Page() {
		var value = [];
		$("input[name='checkall']:checked").each(function() {
			value.push($(this).val());
		});
		var balanceMap = {};
		if('balance' == type){
			var balanceProject='';
			for(var i=0;i<value.length;i++){
			 	balanceProject +=value[i]+",";
			}
			if ($.trim($("#balance_start").val()).length < 1) {
				alert('生效时间不能为空！');
				return null;
			}
			if ($.trim($("#balance_end").val()).length < 1) {
				alert('失效时间不能为空！');
				return null;
			}
			if ($.trim($("#balance_balance").val()).length < 1) {
				alert('余额不能为空！');
				return null;
			}
			if ($.trim(balanceProject).length < 1) {
				alert('产品至少选择一个！');
				return null;
			}
			balanceMap["project"] = balanceProject;
			balanceMap["start"] = $("#balance_start").val();
			balanceMap["end"] = $("#balance_end").val();
			balanceMap["balance"] = $("#balance_balance").val();
			balanceMap["projectId"] = $("#balance_project").val();
			balanceMap["organizationUserid"] = $("#memberid").val();
		}
		/**balance end*/
		
		/**time statr*/
		var timeMap = {};
		if('time' == type){
			var timeProject='';
			for(var i=0;i<value.length;i++){
			 	timeProject +=value[i]+",";
			}
			if ($.trim($("#time_start").val()).length < 1) {
				alert('生效时间不能为空！');
				return null;
			}
			if ($.trim($("#time_end").val()).length < 1) {
				alert('失效时间不能为空！');
				return null;
			}
			if ($.trim(timeProject).length < 1) {
				alert('产品至少选择一个！');
				return null;
			}
			timeMap["project"] = timeProject;
			timeMap["start"] = $("#time_start").val();
			timeMap["end"] = $("#time_end").val();
			timeMap["projectId"] = $("#time_project").val();
			timeMap["organizationUserid"] = $("#memberid").val();
		}
		/**time end*/
		
		/**count statr*/
		var countMap = {};
		if('count' == type){
			var countProject='';
			for(var i=0;i<value.length;i++){
			 	countProject +=value[i]+",";
			}
			if ($.trim($("#count_start").val()).length < 1) {
				alert('生效时间不能为空！');
				return null;
			}
			if ($.trim($("#count_end").val()).length < 1) {
				alert('失效时间不能为空！');
				return null;
			}
			if ($.trim($("#count_surplus").val()).length < 1) {
				alert('剩余次数为空！');
				return null;
			}
			if ($.trim(countProject).length < 1) {
				alert('产品至少选择一个！');
				return null;
			}
			countMap["project"] = countProject;
			countMap["start"] = $("#count_start").val();
			countMap["end"] = $("#count_end").val();
			countMap["surplus"] = $("#count_surplus").val();
			countMap["accumulative"] = '0';
			countMap["projectId"] = $("#count_project").val();
			countMap["organizationUserid"] = $("#memberid").val();
		}
		/**count end*/
		
		$.ajax({
			type : "post",
			url : "organizationUserUpalod.do",
			data : {
				"balanceMap" : JSON.stringify(balanceMap),
				"timeMap" : JSON.stringify(timeMap),
				"countMap" : JSON.stringify(countMap),
				"id" : $("#inputid").val(),
			},
			dataType : "json",
			async : false,
			traditional : true,
			success : function(data) {
				var index = parent.layer.getFrameIndex(window.name);
				window.parent.get();
				parent.layer.msg('修改成功');
				parent.layer.close(index);
			},
			error : function(data) {
				alert("请求失败");
			}
		});
	}
	
	function deleterelation(){
		$.ajax({
			type : "post",
			url : "organizationUserdeleterelation.do",
			data : {
				"projectId":indexid,
				"type":type,
				"memberId":memberId,
				"detailId":detailId,
			},
			dataType : "json",
			async : false,
			traditional : true,
			success : function(data) {
				var index = parent.layer.getFrameIndex(window.name);
				window.parent.get();
				parent.layer.msg('提交成功');
				parent.layer.close(index);
			},
			error : function(data) {
				alert("请求失败");
			}
		});
	}
	//判断时间范围
	 function dateJudge(judgeType,dateProjectId){
	 	var startTimes = $("#"+judgeType+"_start").val();
	 	var mendId = $("#memberid").val();
	 	$.ajax({
			type : "post",
			url : "goDateJudge.do",
			data : {
				"startTimes" : startTimes,
				"mendId" : mendId,
				"dateProjectId" : dateProjectId,
			},
			async : false,
			dataType : "json",
			success : function(data) {
				if(data[0].judge == 'no'){
					alert('该时间段已订购！请重新选择...');
					$("#"+judgeType+"_start").val("");
				}
				if(data[0].judge == 'noNewTime'){
					alert('无法选择已过时间！请重新选择...');
					$("#"+judgeType+"_start").val("");
				}
			},
			error : function(data) {
				alert("请求失败");
			}
		});
	 }
	 //判断时间大小
	 function timeJudge(judgeType){
	 	var startTimes = $("#"+judgeType+"_start").val();
	 	if ($.trim(startTimes).length < 1) {
				alert('请先选择生效时间！');
				$("#"+judgeType+"_end").val("");
				return null;
		}
	 	var endTimes = $("#"+judgeType+"_end").val();
	 	$.ajax({
			type : "post",
			url : "goTimeJudge.do",
			data : {
				"startTimes" : startTimes,
				"endTimes" : endTimes,
			},
			async : false,
			dataType : "json",
			success : function(data) {
				if(data[0].judge == 'no'){
					alert('失效时间必须大于生效时间！');
					$("#"+judgeType+"_end").val("");
				}
			},
			error : function(data) {
				alert("请求失败");
			}
		});
	 }
</script>


